package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.prefix.StrategyStatisKeyPrefix;
import cn.wolfcode.luowowo.cache.service.IRedisService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.IStrategySearchService;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.vo.StatisVo;
import cn.wolfcode.luowowo.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 旅游攻略
 */
@Controller
@RequestMapping("strategy")
public class StrategyController {

    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyContentService strategyContentService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IRedisService redisService;
    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private IStrategySearchService strategySearchService;



    /**
     * 旅游攻略的首要
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("")
    public String index(Model model,Long id){
        //查看前五个
        model.addAttribute("commends",strategyCommendService.queryForCommendTop5(id));



        //数据的排行
        //拿到所有的数据
        List<StrategyStatisVO> all = strategyStatisRedisService.getAllStrategyCommends();
        //创建两个国内国外的list用于存储数据
        List<StrategyStatisVO> abroadCds = new ArrayList<>();
        List<StrategyStatisVO> unabroadCds = new ArrayList<>();

        for (StrategyStatisVO vo : all) {
            if(vo.isIsabroad()){
                if(abroadCds.size()<10){
                    abroadCds.add(vo);
                }
            }else {
                //只要前10个
                if(unabroadCds.size()<10){
                    unabroadCds.add(vo);
                }
            }
        }
        //海外的攻略推荐  abroadCds
        model.addAttribute("abroadCds", abroadCds);

        //国内的攻略推荐  unabroadCds
        model.addAttribute("unabroadCds", unabroadCds);

        //热门的攻略推荐 hotCds
        List<StrategyStatisVO> hotCds = strategyStatisRedisService.getStrategyHotCommon();
        model.addAttribute("hotCds",hotCds.subList(0,10));

        //攻略的主题推荐themeCds
        List<Map<String, Object>> theme = strategySearchService.queryThemeCommends();
        model.addAttribute("themeCds", theme);



        //chinas
        List<StatisVo> chinas = strategySearchService.queryStrategyCondition(SearchQueryObject.CONDITION_TYPE_UN_ABROAD);
        model.addAttribute("chinas", chinas);

        //abroads
        List<StatisVo> abroads = strategySearchService.queryStrategyCondition(SearchQueryObject.CONDITION_TYPE_ABROAD);
        model.addAttribute("abroads", abroads);

        //themes
        List<StatisVo> themes = strategySearchService.queryStrategyCondition(SearchQueryObject.CONDITION_TYPE_THEME);
        model.addAttribute("themes", themes);



        return "strategy/index";
    }

    @RequestMapping("/detail")
    public String detail(Model model, Long id){
        StrategyDetail detail = strategyDetailService.selectDetailById(id);
        detail.setStrategyContent(strategyContentService.selectContentByPrimaryKey(id));
        model.addAttribute("detail",detail);



        //统计阅读数
        strategyStatisRedisService.viewIncrease(StrategyStatisKeyPrefix.STRATEGY_STATIS_VO, id);
        //阅读的排行统计
        strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMOND_HOT,id,1);
        model.addAttribute("vo", strategyStatisRedisService.getStatisVo(id));


        return "strategy/detail";
    }

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo){
        //吐司
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);

        //tags
        model.addAttribute("tags",strategyTagService.selectAll());
        PageInfo pageInfo = strategyDetailService.query(qo);
        //pageInfo
        model.addAttribute("pageInfo",pageInfo);

        return "strategy/list";
    }

    /**
     * 评论的保存操作以及评论数的统计增加操作
     * @param comment
     * @param userInfo
     * @return
     */
    @RequestMapping("/commentAdd")
    @RequiredLogin
    @ResponseBody
    public Object addComment(StrategyComment comment, @UserParam UserInfo userInfo){
        comment.setUserId(userInfo.getId());
        comment.setCity(userInfo.getCity());
        comment.setUserId(userInfo.getId());
        comment.setLevel(userInfo.getLevel());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        strategyCommentService.save(comment);

        //评论数
        strategyStatisRedisService.replyIncrease(StrategyStatisKeyPrefix.STRATEGY_STATIS_VO,comment.getDetailId());

        //评论数排行
        strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMOND_HOT, comment.getDetailId(), 1);

        //返回一个样式,让前台的评论数相加
        return new AjaxResult().addData(strategyStatisRedisService.getStatisVo(comment.getDetailId()).getReplynum());
    }

    /**
     * 评论的分页显示
     * @param model
     * @param
     * @return
     */
    @RequestMapping("/comment")

    public String comment(Model model, @ModelAttribute("qo")StrategyCommentQuery qo){
        //spring data 提供的分页封装对象 :类似,PageResult
        Page<StrategyComment> page = strategyCommentService.query(qo);
        model.addAttribute("page", page);
        return "strategy/commentTpl";
    }

    /**
     * 评论点赞
     * @param toid 评论ID
     * @param fromid 点赞人的ID
     * @return
     */
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    @RequiredLogin
    public Object commentThumbUp(String toid, Long fromid) {
        boolean ret = strategyCommentService.thumbUp(toid, fromid);




        return new AjaxResult(ret, "");
    }


    /**
     * 收藏攻略
     * @param sid 攻略的id
     * @return
     */
    @RequestMapping("/favor")
    @ResponseBody
    @RequiredLogin
    public Object favor(Long sid,@UserParam UserInfo userInfo) {
        if(userInfo ==null){
            return new AjaxResult("请先登录再操作",102 );
        }
        boolean ret = strategyStatisRedisService
                .favorNum(StrategyStatisKeyPrefix.STRATEGY_STATIS_FAVOR,sid,userInfo.getId());
        //收藏时,分数=1  取消收藏的时候 分数-1
        if(ret){
            strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMOND_SORT, sid,ret?1:-1);
        }


        return new AjaxResult(ret,"")
                .addData(strategyStatisRedisService.getStatisVo(sid));
    }
    /**
     * 顶的操作
     * @param sid 攻略的id
     * @return
     */
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    @RequiredLogin
    public Object strategyThumbup(Long sid,@UserParam UserInfo userInfo) {

        boolean ret = strategyStatisRedisService.Thumbup(StrategyStatisKeyPrefix.STRATEGY_STATIS_THUMBUP, sid, userInfo.getId());

        if(ret){
            //参数:攻略 id
            strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMOND_SORT,sid,1);
        }
        return new AjaxResult(ret, "").addData(strategyStatisRedisService.getStatisVo(sid));
    }

    //searchPage
    @RequestMapping("searchPage")
    public String searchPage(Model model,@ModelAttribute("qo")SearchQueryObject qo){
        Page<StrategyTemplate> page = strategySearchService.query(qo);
        model.addAttribute("page",page);
        return "/strategy/searchPageTpl";
    }


}
